package com.example.gorannet;

import java.util.ArrayList;
import java.util.List;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class Tickets extends Fragment implements OnItemClickListener {

	private FragmentManager manager;
	private FragmentTransaction transaction;
	private AlertDialogManager alert             = new AlertDialogManager();
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) 
	{
		View view = inflater.inflate(R.layout.mychoicelist,container, false);
		
		ListView listView = (ListView)view.findViewById(R.id.lv11);
        List<String> listData = new ArrayList<String>();
        DBHandler dbHandler = new DBHandler(getActivity());
        listData = dbHandler.getTickets();
        listView.setAdapter(new ArrayAdapter<String>(getActivity(), R.layout.list_item, R.id.listItem, listData));
        listView.setOnItemClickListener(this);
        
		manager = getFragmentManager();
		return view;

	}
	static String ticket = "";
	String     nPid[] = new String[1];
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		try{
			String pid = ((TextView)arg1.findViewById(R.id.listItem)).getText().toString();
			nPid = new String[1];
	        nPid = pid.split(" ");
	        ticket = nPid[0];
	        if(ticket.equals("Empty")){
	        	alert.showAlertDialog(getActivity(), "Warning",
	                    "No Ticket Found!", false);
	        }
	        else{
			transaction = manager.beginTransaction();
			transaction.replace(R.id.fragmentcontainer2, new TicketDetails());
			transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
			transaction.addToBackStack(getTag());
			transaction.commit();
	        }
		}
		catch(Exception e){
			alert.showAlertDialog(getActivity(), "Warning",
                    "No Ticket Found!", false);
		}
		
	}

}